﻿Imports System.IO

Public Class XFormCalificarParticipante
    Private IDParticipante As Integer

    Public Property IDParticipante1() As Integer
        Get
            Return IDParticipante
        End Get
        Set(value As Integer)
            IDParticipante = value
        End Set
    End Property

    Sub ObtenerDatosParticipantes()
        Using TaDatos As New DsEventoTableAdapters.DatosParticipanteTableAdapter
            Dim DtDatos As DataTable = TaDatos.GetData(IDParticipante)

            lblNombre.Text = String.Format("{0} {1}", DtDatos(0)(0), DtDatos(0)(1))
            LblEdad.Text = String.Format("{0} años", CalcularEdad(DtDatos(0)(2)))
            lblCarrera.Text = DtDatos(0)(4)

            If Not DtDatos(0)(3) Is DBNull.Value Then
                Dim Fotografia As Byte() = DtDatos(0)(3)
                pbFotografia.Image = Image.FromStream(New MemoryStream(Fotografia))
            End If
        End Using
    End Sub

    Sub VerificarSiExisteRegistro()
        Using TaEval As New DsEventoTableAdapters.EvaluacionTableAdapter
            Dim DtEval As DataTable = TaEval.GetDataByIDJP(JuezActivo.IDJuez, IDParticipante)

            If DtEval.Rows.Count = 0 Then
                Try
                    Using TaPasarelas As New DsEventoTableAdapters.PasarelasTableAdapter
                        Dim DtPasarelas As DataTable = TaPasarelas.GetData

                        For Each Pasarela As DataRow In DtPasarelas.Rows
                            EvaluacionTableAdapter.InsertEvaluacion(JuezActivo.IDJuez, IDParticipante, Pasarela("IDPasarela"))
                        Next
                    End Using
                    'EvaluacionTableAdapter.FillByIDJP(DsEvento.Evaluacion, JuezActivo.IDJuez, IDParticipante)
                Catch ex As Exception
                    'MsgBox(ex.Message)
                End Try
            End If
        End Using
    End Sub

    Private Sub CargarCalificacionPorPasarela(ByVal IDPasarela As Integer)
        Using DtEval As DataTable = EvaluacionTableAdapter.GetDataByIDJuezParticipantePasarela(IDParticipante, JuezActivo.IDJuez, IDPasarela)

            EvaluacionTableAdapter.FillByIDJuezParticipantePasarela(DsEvento.Evaluacion, IDParticipante, JuezActivo.IDJuez, IDPasarela)

            If Not DtEval(0)(2) Is DBNull.Value Then
                Eval1TextEdit.Properties.ReadOnly = True
            Else
                Eval1TextEdit.Properties.ReadOnly = False
            End If

            If Not DtEval(0)(3) Is DBNull.Value Then
                Eval2TextEdit.Properties.ReadOnly = True
            Else
                Eval2TextEdit.Properties.ReadOnly = False
            End If

            If Not DtEval(0)(4) Is DBNull.Value Then
                Eval3TextEdit.Properties.ReadOnly = True
            Else
                Eval3TextEdit.Properties.ReadOnly = False
            End If

            If Not DtEval(0)(5) Is DBNull.Value Then
                Eval4TextEdit.Properties.ReadOnly = True
            Else
                Eval4TextEdit.Properties.ReadOnly = False
            End If

            If Not DtEval(0)(6) Is DBNull.Value Then
                Eval5TextEdit.Properties.ReadOnly = True
            Else
                Eval5TextEdit.Properties.ReadOnly = False
            End If

            Using TaTotal As New DsEventoTableAdapters.EvaluacionXPJTableAdapter
                Dim DtTotal As DataTable = TaTotal.GetData(IDParticipante, JuezActivo.IDJuez, cboPasarales.SelectedValue)
                LabelControlTotal.Text = String.Format("Total: {0}", DtTotal(0)(0).ToString)
            End Using
        End Using
    End Sub

    Private Sub MostrarControles(ByVal TF As Boolean)
        Eval1TextEdit.Visible = TF
        Eval2TextEdit.Visible = TF
        Eval3TextEdit.Visible = TF
        Eval4TextEdit.Visible = TF
        Eval5TextEdit.Visible = TF

        Label5.Visible = TF
        Label6.Visible = TF
        Label7.Visible = TF
        Label8.Visible = TF
        Label9.Visible = TF

        LabelControlTotal.Visible = TF
    End Sub


    Private Sub XFormCalificarParticipante_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load, Me.Activated
        PasarelasTableAdapter.Fill(DsEvento.Pasarelas)
        cboPasarales.SelectedIndex = -1

        VerificarSiExisteRegistro()
        ObtenerDatosParticipantes()
        MostrarControles(False)
    End Sub



    Private Sub Eval1TextEdit_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Eval1TextEdit.KeyPress, _
                                                                                                                     Eval2TextEdit.KeyPress, _
                                                                                                                     Eval3TextEdit.KeyPress, _
                                                                                                                     Eval4TextEdit.KeyPress, _
                                                                                                                     Eval5TextEdit.KeyPress, _
                                                                                                                     txtCultura.KeyPress
        If e.KeyChar = ChrW(13) Then
            Dim TextEdit As DevExpress.XtraEditors.TextEdit = CType(sender, DevExpress.XtraEditors.TextEdit)

            Select Case TextEdit.Name
                Case "Eval1TextEdit"
                    If TextEdit.EditValue Is DBNull.Value Then
                        TextEdit.Focus()
                        Exit Sub
                    ElseIf TextEdit.EditValue < 0 Or TextEdit.EditValue > 5 Then
                        TextEdit.EditValue = DBNull.Value
                        TextEdit.Focus()
                        Exit Sub
                    End If
                    EvaluacionTableAdapter.UpdateEval1(CInt(TextEdit.EditValue), JuezActivo.IDJuez, IDParticipante, cboPasarales.SelectedValue)
                    TextEdit.Properties.ReadOnly = True
                    SendKeys.Send("{TAB}")
                Case "Eval2TextEdit"
                    If TextEdit.EditValue Is DBNull.Value Then
                        TextEdit.Focus()
                        Exit Sub
                    ElseIf TextEdit.EditValue < 0 Or TextEdit.EditValue > 5 Then
                        TextEdit.EditValue = DBNull.Value
                        TextEdit.Focus()
                        Exit Sub
                    End If
                    EvaluacionTableAdapter.UpdateEval2(CInt(TextEdit.EditValue), JuezActivo.IDJuez, IDParticipante, cboPasarales.SelectedValue)
                    TextEdit.Properties.ReadOnly = True
                    SendKeys.Send("{TAB}")
                Case "Eval3TextEdit"
                    If TextEdit.EditValue Is DBNull.Value Then
                        TextEdit.Focus()
                        Exit Sub
                    ElseIf TextEdit.EditValue < 0 Or TextEdit.EditValue > 5 Then
                        TextEdit.EditValue = DBNull.Value
                        TextEdit.Focus()
                        Exit Sub
                    End If
                    EvaluacionTableAdapter.UpdateEval3(CInt(TextEdit.EditValue), JuezActivo.IDJuez, IDParticipante, cboPasarales.SelectedValue)
                    TextEdit.Properties.ReadOnly = True
                    SendKeys.Send("{TAB}")
                Case "Eval4TextEdit"
                    If TextEdit.EditValue Is DBNull.Value Then
                        TextEdit.Focus()
                        Exit Sub
                    ElseIf TextEdit.EditValue < 0 Or TextEdit.EditValue > 5 Then
                        TextEdit.EditValue = DBNull.Value
                        TextEdit.Focus()
                        Exit Sub
                    End If
                    EvaluacionTableAdapter.UpdateEval4(CInt(TextEdit.EditValue), JuezActivo.IDJuez, IDParticipante, cboPasarales.SelectedValue)
                    TextEdit.Properties.ReadOnly = True
                    SendKeys.Send("{TAB}")
                Case "Eval5TextEdit"
                    If TextEdit.EditValue Is DBNull.Value Then
                        TextEdit.Focus()
                        Exit Sub
                    ElseIf TextEdit.EditValue < 0 Or TextEdit.EditValue > 5 Then
                        TextEdit.EditValue = DBNull.Value
                        TextEdit.Focus()
                        Exit Sub
                    End If
                    EvaluacionTableAdapter.UpdateEval5(CInt(TextEdit.EditValue), JuezActivo.IDJuez, IDParticipante, cboPasarales.SelectedValue)
                    TextEdit.Properties.ReadOnly = True
                Case "txtCultura"
                    If TextEdit.EditValue Is DBNull.Value Then
                        TextEdit.Focus()
                        Exit Sub
                    ElseIf TextEdit.EditValue < 0 Or TextEdit.EditValue > 25 Then
                        TextEdit.EditValue = DBNull.Value
                        TextEdit.Focus()
                        Exit Sub
                    End If
                    EvaluacionTableAdapter.UpdateCultura(CInt(TextEdit.EditValue), JuezActivo.IDJuez, IDParticipante, cboPasarales.SelectedValue)
                    TextEdit.Properties.ReadOnly = True
            End Select

            Dim num As Integer = 0
            For Each Control In GroupControl2.Controls 'Se recorren todos los controles que estan dentro del GroupControl2
                If Control.GetType() Is GetType(DevExpress.XtraEditors.TextEdit) Then 'Se verifica que el tipo de control es un TextEdit
                    Dim TextBox As DevExpress.XtraEditors.TextEdit = CType(Control, DevExpress.XtraEditors.TextEdit) 'Se hace un cast del control a un Textedit y poder acceder a todas sus propiedades
                    If TextBox.Properties.ReadOnly Then 'Si el control tiene la propiedad ReadOnly en True entonces se incrementa en 1 el acumulador
                        num += 1
                    End If
                End If
            Next

            If num = 5 Then 'Si el acumulador es 5 significa que todos los TextEdit están en ReadOnly
                'MsgBox("Se han evaluado todos los criterios", MsgBoxStyle.Information, "Evaluación terminada")
                Using TaTotal As New DsEventoTableAdapters.EvaluacionXPJTableAdapter
                    Dim DtTotal As DataTable = TaTotal.GetData(IDParticipante, JuezActivo.IDJuez, cboPasarales.SelectedValue)
                    Using MessageBox As New XBigMessageBox
                        With MessageBox
                            .lblTipoTraje.Text = cboPasarales.Text
                            .lblTotal.Text = DtTotal(0)(0).ToString
                            .Text = String.Format("Evaluación de {0}", lblNombre.Text)
                            .ShowDialog()
                        End With
                    End Using
                    LabelControlTotal.Text = String.Format("Total: {0}", DtTotal(0)(0).ToString)
                End Using
                cboPasarales.Focus()
            End If
        End If
    End Sub

    Private Sub cboPasarales_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboPasarales.SelectedIndexChanged
        Dim combo As ComboBox = CType(sender, ComboBox)

        If combo.Text = "Cultura" Then
            MostrarControles(False)
            txtCultura.Visible = True
            lblCultura.Visible = True

            Using DtEval As DataTable = EvaluacionTableAdapter.GetDataByIDJuezParticipantePasarela(IDParticipante, JuezActivo.IDJuez, cboPasarales.SelectedValue)
                EvaluacionTableAdapter.FillByIDJuezParticipantePasarela(DsEvento.Evaluacion, IDParticipante, JuezActivo.IDJuez, cboPasarales.SelectedValue)

                If Not DtEval(0)(8) Is DBNull.Value Then
                    txtCultura.Properties.ReadOnly = True
                Else
                    txtCultura.Properties.ReadOnly = False
                End If
            End Using
            txtCultura.Focus()
        Else
            Try
                If combo.Text = "" Then 'Este evento se dispara al cargar el Form, por esa razón comprobamos el Text en vacío para que ejecute el código de CargarCalificacionPorPasarela
                    Exit Sub
                End If
                MostrarControles(True)
                txtCultura.Visible = False
                lblCultura.Visible = False
                CargarCalificacionPorPasarela(cboPasarales.SelectedValue)
                Eval1TextEdit.Focus()
            Catch ex As Exception
            End Try
        End If
    End Sub
End Class